package com.cloud.pole.rpc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ace.cache.api.CacheAPI;
import com.cloud.pole.advert.model.entity.Advert;
import com.cloud.pole.advert.service.AdvertService;
import com.cloud.pole.api.advert.AdvertApi;
import com.cloud.pole.common.constant.CacheConstant;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.BeanUtils;
import com.cloud.pole.common.util.ListUtils;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("api/advert")
public class AdvertRest {
	
	@Autowired
	AdvertService advertService;

	@RequestMapping(value = "/getAdvertList")
    @ResponseBody
    public ApiRestResponse<List<AdvertApi>> getAdvertList(
    		@RequestParam(defaultValue = "0") Integer advertPosId){
		try {
			List<Advert> srcList = advertService.getAdvertList(advertPosId);
			List<AdvertApi> advertVoList = ListUtils.copyListProperties(srcList, AdvertApi.class);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, advertVoList);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@RequestMapping(value = "/getShopAdvertList")
    @ResponseBody
    public ApiRestResponse<List<AdvertApi>> getShopAdvertList(
    		@RequestParam(defaultValue = "0") Integer shopId,
    		@RequestParam(defaultValue = "0") Integer advertPosId){
		try {
			List<Advert> srcList = advertService.getAdvertListByShopId(shopId, advertPosId);
			List<AdvertApi> destList = ListUtils.copyListProperties(srcList, AdvertApi.class);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, destList);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@RequestMapping(value = "/modifyShopAdvertList")
    @ResponseBody
    public ApiRestResponse<Boolean> modifyShopAdvertList(
    		@RequestParam(defaultValue = "0") Integer shopId,
    		@RequestParam(defaultValue = "0") Integer merchantId,
    		@RequestParam(defaultValue = "0") Integer advertPosId,
    		@RequestParam List<String> advertList
    		){
		try {
			advertService.modifyShopAdvertList(shopId, merchantId, advertPosId, advertList);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, true);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
